<div class="row-fluid">
    <?php echo $this->render('/listados/menu.phtml'); ?>


    <div class="well well-small">
        <legend>Buscar creditos entre fechas por Estados y Tipos</legend>
         <div class="pull-right">
             <label>Vuelta de dinero</label>
             <input type="text" value="<?php echo number_format($this->porcentaje, 2, ',', ''); ?> %" disabled style="width:50px; text-align:right;" />
         </div>
        <form name="form_busqueda" class="noPrint" method="GET" action="<?php echo $this->baseUrl(); ?>/admin/listados/creditosporestado">
           
            <div class="row-fluid">
                <div class="control-group" style="margin-bottom:0px;display:inline-block;">
                    <label class="control-label">Desde</label>
                    <input id="desde" name="desde" value="<?php if (empty($this->desde)) { echo '1'; } else { echo $this->desde; } ?>" style="width:75px;" type="text" />
                </div>
                <div class="control-group" style="margin-bottom:0px;display:inline-block;">
                    <label class="control-label">Hasta</label>
                    <input id="hasta" name="hasta"   value="<?php if (empty($this->hasta)) { echo '1'; } else { echo $this->hasta; } ?>" style="width:75px;" type="text" />
                </div>
                <div class="control-group" style="margin-bottom:0px;display:inline-block;">
                    <label>Plan</label>
                    <select name="plan" class="input-small">
                        <option value="0" <?php echo $this->plan == 0? "selected" : "" ?>>Todos</option>
                        <option value="1" <?php echo $this->plan == 1? "selected" : "" ?>>STD</option>
                        <option value="2" <?php echo $this->plan == 2? "selected" : "" ?>>VBL</option>
                        <option value="3" <?php echo $this->plan == 3? "selected" : "" ?>>MNL</option>
                    </select>
                </div>
                <div class="control-group" style="margin-bottom:0px;display:inline-block;">
                    <label>Tipos</label>
                    <select name="tipo" class="input-medium">
                        <option value="0" <?php echo $this->tipo == 0 ? "selected" : "" ?>>Todos</option>
                        <option value="1" <?php echo $this->tipo == 1 ? "selected" : "" ?>>Nuevo</option>
                        <option value="2" <?php echo $this->tipo == 2 ? "selected" : "" ?>>Renovado</option>
                        <option value="3" <?php echo $this->tipo == 3 ? "selected" : "" ?>>Paralelo</option>
                        <option value="4" <?php echo $this->tipo == 4 ? "selected" : "" ?>>Reingreso</option>
                        <option value="6" <?php echo $this->tipo == 6 ? "selected" : "" ?>>Exclusivo</option>
                    </select>
                </div>
                <div class="control-group" style="margin-bottom:0px;display:inline-block;">
                    <label>Estados</label>
                    <select name="estado" class="input-medium">
                        <option value="todos" <?php echo $this->estado == "todos" ? "selected" : "" ?>>Todos</option>
                        <option value="activos" <?php echo $this->estado == "activos" ? "selected" : "" ?>>Activos</option>
                        <option value="cancelados" <?php echo $this->estado == "cancelados" ? "selected" : "" ?>>Cancelados</option>
                    </select>
                </div>
                <div class="control-group" style="display:inline-block;">
                    <input style="margin-bottom:8px;" class="btn" value="Buscar" type="submit" />
                </div>
            </div>
        </form>
        
        <div class="row-fluid">
            <span>
            <?php
            switch ($this->estado) {
                case 'todos':
                    $subtitulo='<b>'.count($this->creditos).'</b> Creditos encontrados entre <b>'.$this->desde.'</b> a <b>'.$this->hasta.'</b>';
                    break;
                case 'activos':
                    $subtitulo='<b>'.count($this->creditos).'</b> Creditos <b>ACTIVOS</b> encontrados entre <b>'.$this->desde.'</b> a <b>'.$this->hasta.'</b>';
                    break;
                case 'cancelados':
                    $subtitulo='<b>'.count($this->creditos).'</b> Creditos <b>CANCELADOS</b> encontrados entre <b>'.$this->desde.'</b> a <b>'.$this->hasta.'</b>';
                    break;
                default:
                    break;
            }
            echo  $subtitulo;
            ?>
            </span>
            <button type="button" onclick="PrintElement('#tabla_por_estados')" class="btn btn-inverse noPrint" style="float:right; margin:3px;"><i class=" icon-print icon-white"></i>  Imprimir</button>&nbsp;&nbsp;
        </div>
        
        <div class="row-fluid ">
            <table id="tabla_por_estados" class="tabla_reducida">
                <thead>
                <th>DNI</th><th>Nombre</th><th>Apellido</th><th>N&ordm; Cred.</th><th>Cuotas</th><th>Otorgado</th><th>Monto</th><th>Plan</th><th>Tipo</th><th>Estado</th></th>
                </thead>
                <tbody>
                <?php if (count($this->creditos)) { ?>
                    <?php $cant_cuotas_total = 0; $credito = new Doctrine_Model_Entities_Credito() ?>
                    <?php foreach ($this->creditos as $credito) { ?>
                            <?php $cant_cuotas_total += $credito->getCuotas()->count() ?>
                            <tr id="<?php echo "credito{$credito->getId()}" ?>">
                                <td style="width:90px;text-align:center;">
                                    <a href="<?php echo $this->baseUrl("usuarios/busqueda/obternerdatospersona/persona_id/{$credito->getCliente()->getId()}") ?>" onclick="agregaMarcaUrl('<?php echo "#credito{$credito->getId()}" ?>')">
                                        <?php echo $credito->getCliente()->getNroDocumento(); ?>
                                    </a>    
                                </td>
                                <td style="text-align:left;"><?php echo $credito->getCliente()->getNombres(); ?></td>
                                <td style="text-align:left;"><?php echo $credito->getCliente()->getApellido(); ?></td>
                                <td><?php echo $credito->getNumero(); ?></td>
                                <td><?php echo $credito->getCuotas()->count() ?></td>
                                <td><?php echo $credito->getFecha()->format('d/m/Y'); ?></td>
                                <td><?php echo $credito->getMonto(); ?></td>
                                
                                <td><?php echo $credito->getTasa()->getFormula()->getDescripcion() ?></td>
                                <td><?php echo $credito->getTipo()->getDescripcion() ?></td>
                                
                                <td><?php echo $credito->getEstado()->getDescripcion(); ?></td>
                            </tr>
                    <?php } ?>
                            <tr style="background-color: silver;">
                                <td colspan="4"></td>
                                <td><?php echo $cant_cuotas_total ?></td>
                                <td colspan="5"></td>
                            </tr>
                <?php } else { ?>
                        <tr>
                            <td colspan="10">Sin Resultados</td>
                        </tr>
                <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>


<script type="text/javascript">
    $(function() {
        $("#desde").datepicker({dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true});
        $( "#hasta" ).datepicker({dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true});
        
        $("#tabla_por_estados").chromatable({
            width: "100%",  
            height: "350px",
            scrolling: "yes"
        });
        
    });

    function PrintElement(elem){
        Popup($(elem).html());
    }

    //Creates a new window and populates it with your content
    function Popup(data) {
        //Create your new window
        var w = window.open('', 'Creditos <?php echo $this->desde ?> - <?php echo $this->hasta ?>', 'height=600,width=800');
        w.document.write('<html><head><title>Creditos <?php echo $this->desde ?> - <?php echo $this->hasta ?></title>');
        //Include your stylesheet (optional)
        w.document.write('<link rel="stylesheet" href="<?php echo $this->baseUrl("/bootstrap/css/bootstrap.css") ?>" type="text/css" />');
        w.document.write('</head><body>');
        w.document.write('<h2>Creditos <?php echo $this->desde ?> - <?php echo $this->hasta ?></h2>');
        //Write your content
        w.document.write("<table class='table table-bordered'>"+data+"</table>");
        w.document.write('</body></html>');
        w.print();
        w.close();

        return true;
    }
</script>